﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System;

namespace Fulcrum.DispatcherProxies
{
    [Export(typeof(GenerationUnit))]
    public class DispatcherProxyGenerationUnit : GenerationUnit
    {
        DispatcherProxySettings settings;
        List<GenerationDataContext> dataContexts;

        public DispatcherProxyGenerationUnit() :
            base("DispatcherProxy")
        {
            this.settings = new DispatcherProxySettings();
            this.Settings = this.settings;
        }

        public override void CreateDataContexts()
        {
            this.dataContexts = new List<GenerationDataContext>();

            var targetManager = new DispatcherProxyTargetManager();
            targetManager.AddTargetAssembly(this.settings.TargetAssembly);

            foreach (var target in targetManager.Targets)
            {
                var dataContext = new DispatcherProxyGenerationDataContext(this.settings, target);
                this.dataContexts.Add(dataContext);
            }

            this.dataContexts.Sort((a, b) => String.Compare(a.Name, b.Name, StringComparison.InvariantCulture));
        }

        public override ICollection<GenerationDataContext> DataContexts
        {
            get
            {
                return this.dataContexts;
            }
        }
    }
}
